package com.neusoft.shopingapi.dao;

import com.neusoft.shopingapi.comm.Page;
import com.neusoft.shopingapi.entity.GoodsEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class GoodsDAOImpl {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    /**
     * 过得总记录数
     * @return
     */
    public Integer queryCount(){
        String sql01 = "select count(*) from goodsinfo";
        return jdbcTemplate.queryForObject(sql01,Integer.class);
    }
    /**
     * 获取查询分页结果集
     * @param page
     * @return
     */
    public Page<GoodsEntity> queryPage(Page<GoodsEntity> page){
        page.setTotalCount(queryCount());
        String sql = "select * from goodsinfo limit ?,?";
        RowMapper<GoodsEntity> rowMapper = new BeanPropertyRowMapper<>(GoodsEntity.class);
        List<GoodsEntity> list = jdbcTemplate.query(sql,rowMapper,(page.getCurenPage()-1)*page.getPageSize(),page.getPageSize());
        page.setArrys(list);
        return page;
    }

    public GoodsEntity getGoodsByOne(Integer goodsno){
        String sql = "select * from goodsinfo where goodsno=?";
        RowMapper<GoodsEntity> rowMapper = new BeanPropertyRowMapper<>(GoodsEntity.class);
        return jdbcTemplate.queryForObject(sql,rowMapper,goodsno);
    }
    public List<GoodsEntity> getGoodsByType(Integer typeno){
        String sql = "select * from goodsinfo where goodstype=?";
        RowMapper<GoodsEntity> rowMapper = new BeanPropertyRowMapper<>(GoodsEntity.class);
        return jdbcTemplate.query(sql,rowMapper,typeno);
    }
}
